package com.lagou.edu.wwyan.nacos.email.service.impl;

import com.lagou.edu.wwyan.nacos.email.service.LagouEmailService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
@DubboService
@Slf4j
public class LagouEmailServiceImpl implements LagouEmailService {
    @Autowired
    private JavaMailSender mailSender;
    @Value("${spring.mail.username}")
    private String from;
    @Override
    public Boolean sendCode(String email, String code) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(from);
            message.setTo(email);
            message.setSubject("登录验证码");
            message.setText(code);
            //测试时注释掉
            mailSender.send(message);
            return true;
        } catch (MailException e) {
            log.error("验证码发送失败：email:{}，code:{}",email,code,e);
            return false;
        }
    }
}
